﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Entity.Items;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Unitity.EntityModifiers;
using Spinning.Spinning.Interface;
using Spinning.Spinning.EntityModifiers;

namespace Spinning.Spinning.Entity
{
    class SingleGamePlayer : PracticePlayer
    {
        int _lifeTime;

        public delegate void OnLifeTimeZeroing();
        public OnLifeTimeZeroing OnLifeTimeZeroingEvent, OnLifeTimeZeroedEvent;

        public delegate void OnShowAttackMessage(String s);
        public OnShowAttackMessage OnShowAttackMessageEvent;

        public override void OnLoad()
        {
            base.OnLoad();
            OnLifeTimeZeroingEvent += onBeginDead;
            
        }

        public bool IsGamePause
        {
            get;
            set;
        }

        public enum AttackType
        {
            Wind, Water, Fire, Default
        }

        public AttackType Attack_Type
        {
            get;
            set;
        }

        public AttackType _attackType;

        public int LifeTime
        {
            get { return _lifeTime; }
            set { _lifeTime = value; }
        }

        public SingleGamePlayer()
            : base()
        {
            _attackType = AttackType.Fire;
            IsGamePause = false;
        }

        public void AddLifeTime(int t)
        {
            _lifeTime = _lifeTime + t;
            MovableTextEntity addText = new MovableTextEntity();
            ResourceManager.ContentRoot = "";
            addText.fontString = "CenturyGothicBold";
            addText.OnLoad();
            addText.Origin = new Vector2(0.5f);
            addText.Position = new Vector2(0, -10);
            addText.Color = Color.Green;
            addText.ZIndex = Constants.debugText;
            addText.Static = false;
            Attach(addText);
            addText.Time = 1;
            addText.Path = new Vector2(0, -30);
            addText.Value = "+" + t;
            if (OnShowAttackMessageEvent != null)
            {
                OnShowAttackMessageEvent("+" + t);
            }
        }

        public void MinusLifeTimeSecond()
        {
            handleLifeTime(1);
        }

        public void MinusLifeTime(int t)
        {
            handleLifeTime(t);
            MovableTextEntity minusText = new MovableTextEntity();
            ResourceManager.ContentRoot = "";
            minusText.fontString = "CenturyGothicBold";
            minusText.OnLoad();
            minusText.Origin = new Vector2(0.5f);
            minusText.Position = new Vector2(0, -10);
            minusText.Color = Color.Red;
            minusText.ZIndex = Constants.debugText;
            minusText.Static = false;
            Attach(minusText);
            minusText.Time = 1;
            minusText.Path = new Vector2(0, -30);
            minusText.Value = "-" + t;
            if (OnShowAttackMessageEvent != null)
            {
                OnShowAttackMessageEvent("-" + t);
            }
        }

        private void handleLifeTime(int t)
        {
            _lifeTime = _lifeTime - t;
            if (_lifeTime <= 0 && OnLifeTimeZeroingEvent != null&&!IsGod)
            {
                _lifeTime = 0;
                OnLifeTimeZeroingEvent();
                OnLifeTimeZeroingEvent = null;
            }
        }

        int waterShield = 3;
        public virtual void OnTouchedByActor(PhysicSprite actor)
        {
            if (actor.Physic_Type == PhysicType.Boss)
            {
                EnemyBoss boss = (EnemyBoss)actor;
                if (Attack_Type == AttackType.Water && waterShield > 0)  //水屬性開盾
                {
                    MovableTextEntity blockText = new MovableTextEntity();
                    ResourceManager.ContentRoot = "";
                    blockText.fontString = "CenturyGothicBold";
                    blockText.OnLoad();
                    blockText.Origin = new Vector2(0.5f);
                    blockText.Position = new Vector2(0, -10);
                    blockText.Color = Color.Black;
                    blockText.ZIndex = Constants.debugText;
                    blockText.Static = false;
                    Attach(blockText);
                    blockText.Time = 1;
                    blockText.Path = new Vector2(0, -30);
                    blockText.Value = "Block!";
                    waterShield--;
                    if (OnShowAttackMessageEvent != null)
                    {
                        OnShowAttackMessageEvent("Block!");
                    }
                }
                else
                {
                    switch (boss._attackType)
                    {
                        case AttackType.Fire:
                            MinusLifeTime(3);
                            break;
                        case AttackType.Water:
                            MinusLifeTime(2);
                            break;
                        case AttackType.Wind:
                            MinusLifeTime(3);
                            break;
                        case AttackType.Default:
                            MinusLifeTime(1);
                            break;
                    }
                }
            }
        }

        public override void BeginContact(PhysicSprite other)
        {
            base.BeginContact(other);

            if (other.Physic_Type == PhysicType.Boss)
            {
                EnemyBoss boss = (EnemyBoss)other;
                boss.OnTouchedByActor(this);
                //boss.MinusLifeTime(3);
                isFlash = true;
            }
            else if (other.Physic_Type == PhysicType.Barrier || other.Physic_Type == PhysicType.WindBlade)
            {
                isFlash = true;
            }
        }

        int tempTimer, tempTimerFlash, flashCounter = 0;
        public bool IsGod = false;
        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            if (!IsGamePause)
            {
                int delta = gameTime.ElapsedGameTime.Milliseconds;
                tempTimer += delta;
                if (tempTimer / 1000 > 1)  //1秒
                {
                    tempTimer = tempTimer % 1000;
                    if (!IsGod)
                        MinusLifeTimeSecond();
                }

                if (isFlash)
                {
                    Alpha = Math.Abs((float)Math.Sin(tempTimerFlash));
                    tempTimerFlash += delta;
                    if (tempTimerFlash / 300 > 1)
                    {
                        tempTimerFlash = tempTimerFlash % 300;
                        flashCounter++;
                        if (flashCounter == 1)
                        {
                            flashCounter = 0;
                            isFlash = false;
                            Alpha = 1;
                        }
                    }
                }
            }
        }

        protected bool isFlash = false;


        void onBeginDead()
        {
            //SetChangeFrameAnimation(new int[] { 1 + Style * 6 });
            IsReplay = false;
            RotationModifier rm = new RotationModifier(0, 3, 0, (float)Math.PI / 2);
            this.AddModifier(rm);
            rm.OnFinish += onEndDead;
            UpdateHandlerListener -= HowToMove;

        }

        void onEndDead(IEntity item, EntityModifier modifier)
        {
            if (OnLifeTimeZeroedEvent != null) {
                OnLifeTimeZeroedEvent();
                OnLifeTimeZeroedEvent = null;
            }
        }


    }
}
